package gui.initiatives;

import gui.EarthPanel;
import javax.swing.Timer;
import simulation.initiatives.EngineBuffer;
import simulation.threads.EngineBufferThread;
import common.Buffer;
import common.Constants;
import common.SimulationSettings;

public class ClockControlBuffer extends AbstractClockControl {
  private static final long serialVersionUID = -5822023900023271362L;
  
  private ClockBufferListener clockUpdater;
  private Timer dateTimeClock;
  private EarthPanel pnlEarth;
  
  public ClockControlBuffer(Buffer buffer, EarthPanel pnlEarth, SimulationSettings settings) {
    super(pnlEarth,settings);
    this.pnlEarth = pnlEarth;
    
    if(settings.isSimulationThreaded()) {
      EngineBufferThread engineT = new EngineBufferThread(buffer, settings);
      Thread t = new Thread(engineT);
      t.setName("SimThread-Buffered");
      t.start();
      engine = engineT;
    }
    else
      engine = new EngineBuffer(buffer, settings);
    
    clockUpdater = new ClockBufferListener(pnlDateTime, pnlEarth, buffer);
    dateTimeClock = new Timer(Constants.DEFAULT_DISPLAY_RATE, clockUpdater); 
  }
  
  public void adjustBufferRate(int newRate) {
    engine.adjustBufferRate(newRate);
  }
  
  public void adjustDisplayRate(int newRate) {
    dateTimeClock.setDelay(newRate);
  }
    
  public void startClock() {
    engine.setSimTimeStep(settings.getSimTimeStep());
    engine.start();
    dateTimeClock.start();
  }
  
  public void pauseClock() {
    dateTimeClock.stop();
    engine.pause();
  }
  
  public void resetClock() {
    dateTimeClock.stop();
    pnlDateTime.reset();
    pnlEarth.reset();
    engine.reset();
  }
  
}
